'''
给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
示例 1：
输入：head = [1,1,2]
输出：[1,2]
示例 2：
输入：head = [1,1,2,3,3]
输出：[1,2,3]
提示：
链表中节点数目在范围 [0, 300] 内
-100 <= Node.val <= 100
题目数据保证链表已经按升序 排列
'''
#定义一个节点类
class ListNode():
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        #链表为空则直接返回
        if not head:
            return head
        cur = head
        nex = cur.next
        while nex:  #遍历链表
            if cur.val == nex.val:
                nex = nex.next
                cur.next = nex
            else:
                cur = cur.next
                nex = nex.next
        return head
#示例
if __name__ == '__main__':
    head = ListNode(2)
    head.next = ListNode(3)
    head.next.next = ListNode(4)
    head.next.next.next = ListNode(4)
    new_head = Solution().deleteDuplicates(head)
    while new_head:
        print(new_head.val,end='->')
        new_head = new_head.next